﻿Public Class UserFlash
    Inherits System.Web.UI.UserControl

    Private _userName As String
    Private _size As UserFlashSize

    Public Property UserName() As String
        Get
            Return If(IsNothing(_userName), "", _userName)
        End Get
        Set(ByVal value As String)
            _userName = value
        End Set
    End Property

    Public Property Size() As UserFlashSize
        Get
            Return If(IsNothing(_size), UserFlashSize.Small, _size)
        End Get
        Set(ByVal value As UserFlashSize)
            _size = value
        End Set
    End Property

    ' N.B. Bind the user controls on the PreRender event, not on the Load event.
    ' If I don't do it this way then the control won't work when it's UserName attribute
    ' is bound inside a ListView.
    ' 
    ' Details at http://stackoverflow.com/questions/15381781/passing-variables-to-usercontrol-inside-the-listview-asp-net-c
    Protected Sub Page_Prerender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim um As UserModel = New UserModel(UserName)
        Picture.ImageUrl = um.Picture
        If (UserName = "") Then
            linkToProfile.Text = "unknown user"
            linkToProfile.Enabled = False
        Else
            linkToProfile.Text = UserName
            linkToProfile.NavigateUrl = "~/viewProfile.aspx?UserName=" & UserName
        End If
        Dim cssClass As String
        If (Size = UserFlashSize.Large) Then
            cssClass = "Large"
        Else
            cssClass = "Small"
        End If
        cssClass = "UserFlash UserFlash" & cssClass
        Panel.CssClass = cssClass
        Picture.CssClass = cssClass
    End Sub

End Class